package com.yao.club.service.impl;

import com.yao.club.entity.dto.GoodsCollectRecord;
import com.yao.club.entity.UserEntity;
import com.yao.club.entity.dto.MallGoodsInfo;
import com.yao.club.entity.result.ResponseEntity;
import com.yao.club.entity.result.ResultEntity;
import com.yao.club.mapper.UserMapper;
import com.yao.club.reposittory.GoodsCollectRecordRepository;
import com.yao.club.reposittory.MallGoodsInfoRepository;
import com.yao.club.service.GoodsCollectService;
import com.yao.club.utils.ResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Instant;
import java.util.List;

@Service
public class GoodsCollectServiceImpl implements GoodsCollectService {

    @Autowired
    private GoodsCollectRecordRepository collectRecordRepository;
    @Autowired
    private MallGoodsInfoRepository mallGoodsInfoRepository;

    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional
    public ResponseEntity<Boolean> toggleCollect(Long goodsId, String username) {
        try {
            // 获取用户信息
            UserEntity user = userMapper.select(username);
            if (user == null) {
                return ResponseUtils.failed("用户不存在");
            }

            //判断商品是否存在
            if (goodsId == null) {
                return ResponseUtils.failed("商品ID不能为空");
            }
            MallGoodsInfo mallGoodsInfo = mallGoodsInfoRepository.findMallGoodsInfosById(goodsId);
            if (mallGoodsInfo ==   null) {
                return ResponseUtils.failed("商品不存在");
            }
            // 查找是否已存在收藏记录
            GoodsCollectRecord existingRecord = collectRecordRepository
                .findByGoodsIdAndUserId(goodsId, user.getMember_id());

            if (existingRecord != null) {
                // 已收藏，执行取消收藏
                collectRecordRepository.delete(existingRecord);
                return ResponseUtils.success(true,"取消收藏成功");
            } else {
                // 未收藏，执行收藏
                GoodsCollectRecord newRecord = new GoodsCollectRecord();
                newRecord.setGoodsId(goodsId);
                newRecord.setUserId(user.getMember_id());
                newRecord.setCreatedAt(Instant.now());
                collectRecordRepository.save(newRecord);
                return ResponseUtils.success(true,"收藏成功");
            }
        } catch (Exception e) {
            return ResponseUtils.failed("操作失败：" + e.getMessage());
        }
    }

    @Override
    public ResponseEntity<Boolean> checkCollectStatus(Long goodsId, String username) {
        try {
            // 获取用户信息
            UserEntity user = userMapper.select(username);
            if (user == null) {
                return ResponseUtils.failed("用户不存在");
            }

            // 查询收藏状态
            GoodsCollectRecord record = collectRecordRepository
                .findByGoodsIdAndUserId(goodsId, user.getMember_id());
            if (record == null) {
                return ResponseUtils.success(false,"未收藏");
            }else {
                return ResponseUtils.success(true,"已经收藏");
            }
        } catch (Exception e) {
            return ResponseUtils.failed("查询收藏状态失败：" + e.getMessage());
        }
    }

    @Override
    public ResponseEntity<ResultEntity<GoodsCollectRecord>> getCollectedGoods(String username) {
        try {
            // 获取用户信息
            UserEntity user = userMapper.select(username);
            if (user == null) {
                return ResponseUtils.failed("用户不存在");
            }
            List<GoodsCollectRecord> collectedGoods = collectRecordRepository.findByUserId(user.getMember_id());
            ResultEntity<GoodsCollectRecord> result = new ResultEntity<>();
            result.setRecords(collectedGoods);
            result.setTotal(collectedGoods.size());
            return ResponseUtils.success(result);
        } catch (Exception e) {
            return ResponseUtils.failed("获取收藏商品失败：" + e.getMessage());
        }
    }
} 